<?php
	
	class Search extends CI_Controller{
		
		function __construct(){
			parent::__construct();
			$this->load->model(array('search_model'));
			$this->load->library(array('solr', 'pagination'));
		}
		
		function index($keyword = NULL, $page = 1){
			//echo $keyword;exit();
			# Get keywords
			if($keyword == "") {
				redirect();
			}else {
				$keyword = preg_replace('/[\s\?\%20]+/is','+',$keyword);
				$keyword = preg_replace('/[\:\?\&\<\>\#\'\"\+]+/is',' ',$keyword);
			}
			# Add data into Solr
			/* $input		=	$this->search_model->get();
			if($input){
				$this->solr->add($input);
			} */
			
			$limit		=	20;
			$offset		=	($page - 1)*$limit;
			$queries 	= 	array();
			$queries[0] = 	$keyword;
			$res		=	$this->solr->search($queries, $offset, $limit);
			
			$url = base_url()."search/".$keyword;
			$total		=	ceil($res['total_num']/$limit); 
  			$conf = array(
  					'total_rows'		=>	$total,
  					'cur_page'			=>	$page,
  					'base_url'			=>	$url,
  					'show_num'			=>	'yes',
  					'show_total' 		=>	'no',
  					'show_first_last'	=>	'yes',
  					'prev_class'		=>	'pic',
  					'next_class'		=>	'pic',
  					'next_link'			=>	'Next >>',
  					'prev_link'			=>	'<< Prev'
  			);
  			$this->pagination->initialize($conf);
  			$data['pagination'] = $this->pagination->non_ajax();
			
			$data['info']	=	$res;
			$this->load->view('search', $data);
		}
	}